// pages/intercourseUnit/intercourseUnit.js

// import pinyin from "wl-pinyin";
import api from "../../utils/api";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        coId: '',
        searchValue: '',
        getList: [], //全部
        indexList: ['#', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'V', 'U', 'W', 'X', 'Y', 'Z'], // 索引
        nodata: false, //是否没有数据
        tips: '暂无数据',
        loading: true,
        remind: '下拉加载更多',
        page: 0, // 当前页码
        pageSize: 10, // 每页显示数据
        leftdel: false,
        // from:'', //从哪个页面跳转来的
        // wlid:'',  //选择的往来单位id
        // wlname:'',//选择的往来单位name

        /////// 上个页面的值保存
        ContractNo: '',
        TaskNumber: '',
        SignedAmount: '',

    },
    clickitem(e) {
        console.log(e, 'item')
        this.setData({
            wlid: e.currentTarget.dataset.id,
            wlname: e.currentTarget.dataset.name
        })
        console.log(this.data.wlid, '选择的往来单位id')
        console.log(this.data.wlname, 'wlname')

       
        // 点击跳转 from =1 跳转到 xxx   form=2 跳转到xxxx
        console.log(this.data.from, 'from111111')
        if (this.data.from == 1) {
            //新建发票修改

            // wx.navigateTo({
            //   url: '/pages/addNewinvoice/addNewinvoice?wlid=' + id + '&wlname=' + name,
            // })

            api.wldwupdate({
                method: 'POST',
                data: {
                    id: this.data.invid,
                    cusId: this.data.wlid,
                },
                success: function (res) {
                    console.log(res.data.data, "新建发票修改")
                    if (res.data.code = 200) {
                        wx.showToast({
                            title: '添加成功',
                        })
                        //返回到上一页
                        setTimeout(() => {
                            wx.navigateBack({
                                delta: 1,
                            })
                        }, 100);
                      
                    }
                }
            })
        } else if (this.data.from == 2) {
            //编辑发票修改
            api.wldwupdate({
                method: 'POST',
                data: {
                    id: this.data.invid,
                    cusId: this.data.wlid,
                },
                success: function (res) {
                    console.log(res.data.data, "编辑发票修改")
                    if (res.data.code = 200) {
                        wx.showToast({
                            title: '添加成功',
                        })
                        //返回到上一页
                        setTimeout(() => {
                            wx.navigateBack({
                                delta: 1,
                            })
                        }, 100);
                      
                    }
                }
            })
        } else {
            // 点击 选择合同号 的“+”跳转到新建合同页面
            wx.navigateTo({
                url: '/pages/addNewbill/addNewbill?id= ' + e.currentTarget.dataset.id + '&name=' + e.currentTarget.dataset.name + '&ContractNo=' + this.data.ContractNo + '&TaskNumber=' + this.data.TaskNumber + '&SignedAmount=' + this.data.SignedAmount,
            })
        }

    },
    //点击 + 新增往来单位
    clickadd_wldw(e) {
        wx.navigateTo({
            url: '/pages/addIntercourseUnit/addIntercourseUnit',
        })
    },
    //搜索
    onSearch(event) {
        this.setData({
            searchValue: event.detail && event.detail.value ? event.detail.value : event.detail,
            loading: true,
        })
        this.clearData()
    },
    //取消搜索
    onCancel() {
        console.log(1111)
        this.getList()
    },
    // letterClick(e){
    //     this.setData({
    //         searchValue: e.currentTarget.dataset.item,
    //         loading: true,
    //     })
    //     this.clearData()
    // },
    //获取全部列表
    getList() {
        let that = this;
        that.setData({
            page: this.data.page + 1
        })
        api.getWldwList({
            data: {
                coId: this.data.coId,
                searchStr: that.data.searchValue ? that.data.searchValue : '',
                pageNum: this.data.page,
                pageSize: that.data.pageSize
            },
            success: function (res) {
                console.log(res.data.data, "getWldwList")
                if (res.data.code == 200) {
                    if (res.data.data.list.length > 0) {
                        for (let i = 0; i < res.data.data.list.length; i++) {
                            that.data.indexList.push(res.data.data.list[i].initials)
                        }
                        let newArr = [...new Set(that.data.indexList)] // 去重
                        that.data.totalPage = res.data.data.totalPage
                        that.setData({
                            indexList: newArr,
                            getList: that.data.getList.concat(res.data.data.list),
                            loading: false,
                            nodata: false,
                            remind: res.data.data.totalPage > res.data.data.pageNum ? '下拉加载更多' : '已经到底啦～',
                        })
                    } else {
                        that.setData({
                            nodata: true
                        })
                    }
                }
            }
        })
    },
    // 清空数据并重新加载数据
    clearData() {
        this.setData({
            getList: [],
            loading: true,
            page: 0,
        })
        this.getList()
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options, 'from...')
        this.setData({
            from: options.from,
            invid: options.invid
        })
        console.log(this.data.from, 'from111111')
        console.log(this.data.invid, 'invid111111')


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            ContractNo: wx.getStorageSync('ContractNo'),
            TaskNumber: wx.getStorageSync('TaskNumber'),
            SignedAmount: wx.getStorageSync('SignedAmount'),
        })
        this.data.coId = wx.getStorageSync('userInfo').coId
        this.getList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        console.log('123')
        if (this.data.totalPage > this.data.page) {
            // ++this.data.page;
            this.setData({
                loading: true
            })
            this.getList()
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})